package com.ruoyi.web.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.constant.WeChatContans;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.wechat.WeChatLoginVo;
import com.ruoyi.common.utils.wechat.WeChatUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @version :
 * @program: easyBuy
 * @description: 微信相关接口
 * @author: 周爱鸿
 * @create: 2020-08-11 09:45
 **/
@RestController
@RequestMapping("/weChat")
@Api(description = "微信相关接口")
@Slf4j
public class WeChatController {

	@Autowired
	private RestTemplate restTemplate;


	/**
	 * 微信授权
	 * @param code 微信code
	 * @return
	 */
	@GetMapping("/oauth")
	@ApiOperation("微信授权")
	@ApiImplicitParam(name="code",value="微信code",required = true,dataType = "String")
	public AjaxResult auth(String code){
		if (null == code || "".equals(code)){
			return AjaxResult.error("open_id获取失败");
		}
		String url = WeChatContans.OAUTH_APPWX_URL.replace("CODE", code);


		if ( !WeChatContans.REFUSE_CODE.equals(code)){
			JSONObject result =
					restTemplate.getForObject(url, JSONObject.class);
			if (null != result){
				log.info(JSONObject.toJSONString(result));
				return AjaxResult.success(result);
			}
		}
		return AjaxResult.error("open_id获取失败");
	}


}
